using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Text.RegularExpressions;
using System.Xml;
using System.Web;
using System.Web.UI;
using System.IO;

////////////////////////////////////////////////////////////////////////////////////////////////////
// namespace: Dimok
//
// summary:	.
////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Dimok{

	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Extentions. </summary>
	///
	/// <remarks>	Dimon, 11/22/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public static class Extentions {

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	A string extension method that removes the space preserve described by text. </summary>
		///
		/// <remarks>	Dimon, 11/22/2010. </remarks>
		///
		/// <param name="text">	The text to act on. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string RemoveSpacePreserve(this string text) {
			return text.Replace(" xml:space=\"preserve\"", "");
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	A string extension method that makes a header. </summary>
		///
		/// <remarks>	Dimon, 11/22/2010. </remarks>
		///
		/// <param name="Name">	The Name to act on. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string MakeHeader(this string Name) {
			return Regex.Replace(Regex.Replace(Name, "([a-z]+)([A-Z])", "$1 $2"),"_"," ").Trim();
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	A string extension method that defaults. </summary>
		///
		/// <remarks>	Dimon, 11/22/2010. </remarks>
		///
		/// <param name="s">	The s to act on. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string Default(this string s) { return s.Default(null); }

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	A string extension method that defaults. </summary>
		///
		/// <remarks>	Dimon, 11/22/2010. </remarks>
		///
		/// <param name="s">						The s to act on. </param>
		/// <param name="DefaultValue">	Default value. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string Default(this string s, string DefaultValue) {
			return s.Default("", DefaultValue);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	A string extension method that defaults. </summary>
		///
		/// <remarks>	Dimon, 11/22/2010. </remarks>
		///
		/// <param name="s">						The s to act on. </param>
		/// <param name="NullIFValue">	The null if value. </param>
		/// <param name="DefaultValue">	Default value. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string Default(this string s, string NullIFValue, string DefaultValue) {
			return s + "" == NullIFValue ? DefaultValue : s;
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	A Dictionary&lt;string,T&gt; extension method that ensures that value. </summary>
		///
		/// <remarks>	Dimon, 11/22/2010. </remarks>
		///
		/// <typeparam name="T">	Generic type parameter. </typeparam>
		/// <param name="Dic">	The Dic to act on. </param>
		/// <param name="Key">	The key. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static T EnsureValue<T>(this Dictionary<string, T> Dic, string Key) where T : class {
			if (Dic.ContainsKey(Key)) return Dic[Key];
			Dic.Add(Key, Activator.CreateInstance<T>());
			return Dic[Key];
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	A Dictionary&lt;string,string&gt; extension method that trims. </summary>
		///
		/// <remarks>	Dimon, 11/22/2010. </remarks>
		///
		/// <param name="d">	The d to act on. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static Dictionary<string, string> Trim(this Dictionary<string, string> d) {
			return d.Where(f => !string.IsNullOrEmpty(f.Value)).ToDictionary(f => f.Key, f => f.Value);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	An object extension method that converts an o to a dictionary. </summary>
		///
		/// <remarks>	Dimon, 11/22/2010. </remarks>
		///
		/// <param name="o">	The o to act on. </param>
		///
		/// <returns>	This object as a Dictionary&lt;string,object&gt; </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static Dictionary<string, object> ToDictionary(this object o) {
			var d = o.GetType().GetProperties().ToDictionary(s => s.Name, s => o.GetProperty(s.Name));
			return d;
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	An object extension method that converts this object to a dictionary. </summary>
		///
		/// <remarks>	Dimon, 11/22/2010. </remarks>
		///
		/// <typeparam name="T">	Generic type parameter. </typeparam>
		/// <param name="o">						The o to act on. </param>
		/// <param name="defaultValue">	Default value. </param>
		///
		/// <returns>	This object as a Dictionary&lt;string,T&gt; </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static Dictionary<string, T> ToDictionary<T>(this object o, T defaultValue) {
			var d = o.GetType().GetProperties().ToDictionary(s => s.Name, s => (T)(o.GetProperty(s.Name) ?? defaultValue));
			return d;
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	An object extension method that converts an o to a dictionary. </summary>
		///
		/// <remarks>	Dimon, 11/22/2010. </remarks>
		///
		/// <param name="o">						The o to act on. </param>
		/// <param name="defaultValue">	Default value. </param>
		///
		/// <returns>	This object as a Dictionary&lt;string,object&gt; </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static Dictionary<string, string> ToDictionary(this object o, string defaultValue) {
			var d = o.GetType().GetProperties().ToDictionary(s => s.Name, s => (o.GetProperty(s.Name) ?? defaultValue) + "");
			return d;
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	A Type extension method that gets a base name. </summary>
		///
		/// <remarks>	Dimon, 11/22/2010. </remarks>
		///
		/// <param name="t">	The t to act on. </param>
		///
		/// <returns>	The base name. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string GetBaseName(this Type t) {
			return string.Join(".", t.FullName.Split('.').Reverse().Skip(1).Reverse().ToArray());
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	An object extension method that copies this object. </summary>
		///
		/// <remarks>	Dimon, 11/22/2010. </remarks>
		///
		/// <param name="Source">				The Source to act on. </param>
		/// <param name="Destination">	Destination for the. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void Copy(this object Source, object Destination) {
			foreach (PropertyInfo pi in Source.GetType().GetProperties())
				Destination.SetProperty(pi.Name, Source.GetProperty(pi.Name));
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Get properly capitalized property name. </summary>
		///
		/// <remarks>	Dimon, 11/22/2010. </remarks>
		///
		/// <param name="o">														The o to act on. </param>
		/// <param name="CaseInsensitivePropertyName">	Property Name. </param>
		///
		/// <returns>	The property name. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string GetPropertyName(this object o, string CaseInsensitivePropertyName) {
			return GetPropertyName(o.GetType(), CaseInsensitivePropertyName,false);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Get properly capitalized property name. </summary>
		///
		/// <remarks>	Dimon, 11/22/2010. </remarks>
		///
		/// <param name="o">														The o to act on. </param>
		/// <param name="CaseInsensitivePropertyName">	Property Name. </param>
		/// <param name="RaiseError">										Default: false. </param>
		///
		/// <returns>	The property name. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string GetPropertyName(this object o, string CaseInsensitivePropertyName, bool RaiseError) {
			return GetPropertyName(o.GetType(), CaseInsensitivePropertyName,RaiseError);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Get properly capitalized property name. </summary>
		///
		/// <remarks>	Dimon, 11/22/2010. </remarks>
		///
		/// <param name="t">														The t to act on. </param>
		/// <param name="CaseInsensitivePropertyName">	Property Name. </param>
		///
		/// <returns>	The property name. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string GetPropertyName(this Type t, string CaseInsensitivePropertyName) {
			return GetPropertyName(t, CaseInsensitivePropertyName, false);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Get properly capitalized property name. </summary>
		///
		/// <remarks>	Dimon, 11/22/2010. </remarks>
		///
		/// <exception cref="NotImplementedException">	Thrown when the requested operation is
		/// 																						unimplemented. </exception>
		///
		/// <param name="t">														The t to act on. </param>
		/// <param name="CaseInsensitivePropertyName">	Property Name. </param>
		/// <param name="RaiseError">										Default: false. </param>
		///
		/// <returns>	The property name. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string GetPropertyName(this Type t, string CaseInsensitivePropertyName,bool RaiseError) {
			var s = t.GetProperties().Select(p => p.Name).SingleOrDefault(n => n.ToLower() == CaseInsensitivePropertyName.ToLower()) + "";
			if (s == "" && RaiseError) throw new NotImplementedException("Type:"+t.FullName+",Property:" + CaseInsensitivePropertyName);
			return s;
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	An object extension method that try get property. </summary>
		///
		/// <remarks>	Dimon, 11/22/2010. </remarks>
		///
		/// <param name="o">	The o to act on. </param>
		/// <param name="p">	The. </param>
		/// <param name="v">	[out] The v. </param>
		///
		/// <returns>	true if it succeeds, false if it fails. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static bool TryGetProperty(this object o, string p, out object v) {
			System.Reflection.PropertyInfo pi = o.GetType().GetProperty(p);
			if (pi != null) {
				v = pi.GetValue(o, null);
				return true;
			}
			System.Reflection.FieldInfo fi = o.GetType().GetField(p);
			if (fi != null) {
				v = fi.GetValue(o);
				return true;
			}
			v = null;
			return false;
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	An object extension method that gets a property. </summary>
		///
		/// <remarks>	Dimon, 11/22/2010. </remarks>
		///
		/// <exception cref="NotImplementedException">	Thrown when the requested operation is
		/// 																						unimplemented. </exception>
		///
		/// <param name="o">	The o to act on. </param>
		/// <param name="p">	The. </param>
		///
		/// <returns>	The property. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static object GetProperty(this object o, string p) {
			System.Reflection.PropertyInfo pi = o.GetType().GetProperty(p);
			if (pi != null) return pi.GetValue(o, null);
			System.Reflection.FieldInfo fi = o.GetType().GetField(p);
			if (fi != null) return fi.GetValue(o);
			throw new NotImplementedException("Property/Field " + p + " is not implemented in " + o.GetType().Name + ".");
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	An object extension method that gets a property. </summary>
		///
		/// <remarks>	Dimon, 11/22/2010. </remarks>
		///
		/// <exception cref="NotImplementedException">	Thrown when the requested operation is
		/// 																						unimplemented. </exception>
		///
		/// <param name="c">	The c to act on. </param>
		/// <param name="p">	The. </param>
		///
		/// <returns>	The property. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static object GetProperty(this Control c, string p) {
			while (c != null)
				try {
					return ((object)c).GetProperty(p);
				} catch (NotImplementedException) {
					c = ((Control)c).NamingContainer;
				}
			throw new NotImplementedException("Property " + p + " is not implemented.");
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	An object extension method that sets a property. </summary>
		///
		/// <remarks>	Dimon, 11/22/2010. </remarks>
		///
		/// <exception cref="NotImplementedException">	Thrown when the requested operation is
		/// 																						unimplemented. </exception>
		///
		/// <param name="o">	The o to act on. </param>
		/// <param name="p">	The. </param>
		/// <param name="v">	The v. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void SetProperty(this object o, string p, object v) {
			System.Reflection.PropertyInfo pi = o.GetType().GetProperty(p);
			Type tThis = Nullable.GetUnderlyingType(pi.PropertyType);
			if (tThis == null) tThis = pi.PropertyType;
			if (tThis == typeof(System.Double)) v = Convert.ToDouble(v);
			else if (tThis == typeof(System.String)) v = v + "";
			else if (tThis == typeof(System.DateTime)) v = Convert.ToDateTime(v);
			else if (tThis == typeof(System.Decimal)) v = Convert.ToDecimal(v);
			else if (tThis == typeof(System.Int32)) v = Convert.ToInt32(v);
			if (pi != null) pi.SetValue(o, v, new object[] { });
			else {
				System.Reflection.FieldInfo fi = o.GetType().GetField(p);
				if (fi == null) throw new NotImplementedException("Property " + p + " is not implemented in " + o.GetType().FullName + ".");
				fi.SetValue(o, v);
			}
		}
		// public static void SetProperty(this object o, string p, object v) {
		//   System.Reflection.PropertyInfo pi = o.GetType().GetProperty(p);
		//   if (pi != null) pi.SetValue(o, v, new object[] { });
		//   else {
		//     System.Reflection.FieldInfo fi = o.GetType().GetField(p);
		//     if (fi == null) throw new NotImplementedException("Property " + p + " is not implemented
		//     in " + o.GetType().FullName + ".");
		//     fi.SetValue(o, v);
		//   }
		// } 

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	A string extension method that levenshteins. </summary>
		///
		/// <remarks>	Dimon, 11/23/2010. </remarks>
		///
		/// <param name="s">	The s to act on. </param>
		/// <param name="t">	The t to act on. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static int Levenshtein(this string s, string t) {
			int n = s.Length;
			int m = t.Length;
			int[,] d = new int[n + 1, m + 1];
			// Step 1
			if (n == 0) return m;
			if (m == 0) return n;

			// Step 2
			for (int i = 0; i <= n; d[i, 0] = i++) {
			}

			for (int j = 0; j <= m; d[0, j] = j++) {
			}

			// Step 3
			for (int i = 1; i <= n; i++) {
				//Step 4
				for (int j = 1; j <= m; j++) {
					// Step 5
					int cost = (t[j - 1] == s[i - 1]) ? 0 : 1;

					// Step 6
					d[i, j] = Math.Min(
							Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
							d[i - 1, j - 1] + cost);
				}
			}
			// Step 7
			return d[n, m];
		}
	}
	public class Common{

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Dispose them. </summary>
		///
		/// <remarks>	Dimon, 11/23/2010. </remarks>
		///
		/// <param name="refs">	A variable-length parameters list containing references. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void DisposeThem(params object[] refs){
			foreach( object o in refs )
				while( o != null && Marshal.ReleaseComObject(o) > 0 ){}; 
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Trace me. </summary>
		///
		/// <remarks>	Dimon, 11/23/2010. </remarks>
		///
		/// <param name="message">	The message. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void TraceMe(string message){
			StackTrace stack = new StackTrace();
			MethodBase method = stack.GetFrame(1).GetMethod();
			Trace.WriteLine(DateTime.Now.ToString("MM/dd/yy HH:mm:ss.f") + " **** " + method.Name + " : " + message);
			Trace.Flush();
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Trace me. </summary>
		///
		/// <remarks>	Dimon, 11/23/2010. </remarks>
		///
		/// <param name="fileName">	Filename of the file. </param>
		/// <param name="message">	The message. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static void TraceMe(string fileName,string message){
			TextWriterTraceListener twl = new TextWriterTraceListener(fileName,"Dimok");
			StackTrace stack = new StackTrace();
			MethodBase method = stack.GetFrame(1).GetMethod();
			Trace.WriteLine(DateTime.Now.ToString("MM/dd/yy HH:mm:ss.f")+" **** "+ method.Name+" : "+message);
			twl.Flush();
			twl.Close();
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets the func name. </summary>
		///
		/// <remarks>	Dimon, 11/23/2010. </remarks>
		///
		/// <returns>	The func name. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[MethodImplAttribute(MethodImplOptions.NoInlining)]
		public static string GetFuncName(){
			StackTrace st = new StackTrace(1);
			MethodBase m = st.GetFrame(0).GetMethod();
			return m.IsSpecialName ? m.Name.Replace("get_","") : m.Name;
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets the func name. </summary>
		///
		/// <remarks>	Dimon, 11/23/2010. </remarks>
		///
		/// <param name="startFrame">	The start frame. </param>
		///
		/// <returns>	The func name. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[MethodImplAttribute(MethodImplOptions.NoInlining)]
		public static string GetFuncName(int startFrame){
			StackTrace st = new StackTrace(1);
			MethodBase m = st.GetFrame(startFrame).GetMethod();
			return m.IsSpecialName ? m.Name.Replace("get_","") : m.Name;
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets the method. </summary>
		///
		/// <remarks>	Dimon, 11/23/2010. </remarks>
		///
		/// <returns>	The method. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[MethodImplAttribute(MethodImplOptions.NoInlining)]
		public static MethodBase GetMethod(){
			StackTrace st = new StackTrace(1);
			return st.GetFrame(0).GetMethod();
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets the method. </summary>
		///
		/// <remarks>	Dimon, 11/23/2010. </remarks>
		///
		/// <param name="frame">	The frame. </param>
		///
		/// <returns>	The method. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[MethodImplAttribute(MethodImplOptions.NoInlining)]
		public static MethodBase GetMethod(int frame){
			StackTrace st = new StackTrace(1);
			return st.GetFrame(frame).GetMethod();
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Query if 'Expression' is numeric. </summary>
		///
		/// <remarks>	Dimon, 11/23/2010. </remarks>
		///
		/// <param name="Expression">	The expression. </param>
		///
		/// <returns>	true if numeric, false if not. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static bool IsNumeric(object Expression) {
			bool isNum;
			double retNum;
			isNum = double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum );
			return isNum;
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Parse int. </summary>
		///
		/// <remarks>	Dimon, 11/23/2010. </remarks>
		///
		/// <param name="number">	Number of. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static int ParseInt(object number) { return IsNumeric(number) ? int.Parse(number + "") : 0; }

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Parse decimal. </summary>
		///
		/// <remarks>	Dimon, 11/23/2010. </remarks>
		///
		/// <param name="number">	Number of. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static decimal ParseDecimal(object number) { return IsNumeric(number) ? Convert.ToDecimal(number) : 0; }

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Parse bool. </summary>
		///
		/// <remarks>	Dimon, 11/23/2010. </remarks>
		///
		/// <param name="v">	The v. </param>
		///
		/// <returns>	true if it succeeds, false if it fails. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static bool ParseBool(string v){return v.ToLower() == "true";}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets a property. </summary>
		///
		/// <remarks>	Dimon, 11/23/2010. </remarks>
		///
		/// <param name="o">	The. </param>
		///
		/// <returns>	The property. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static object GetProperty(object o) { return GetProperty(o, GetFuncName(2)); }

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets a property. </summary>
		///
		/// <remarks>	Dimon, 11/23/2010. </remarks>
		///
		/// <param name="o">	The. </param>
		/// <param name="p">	The. </param>
		///
		/// <returns>	The property. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static object GetProperty(object o, string p) {
			System.Reflection.PropertyInfo pi = o.GetType().GetProperty(p);
			return pi == null ? null : pi.GetValue(o, null);
		}

		#region BuildSelectOptions
		/*
		public static string BuildSelectOptions(DataSet ds,string textField,string valueField){
			ds.Tables[0].TableName = "OPTION";
			foreach( DataColumn col in ds.Tables[0].Columns )
				if( col.ColumnName.ToLower() == textField.ToLower() )col.ColumnMapping = MappingType.SimpleContent;
				else if( col.ColumnName.ToLower() == valueField.ToLower() )col.ColumnMapping = MappingType.Attribute;
				else col.ColumnMapping = MappingType.Hidden;
			return Regex.Match(ds.GetXml(),@"^<[^>]+>([\s\S]*)<[^>]+>$").Groups[1].Value;
		}
		public static string BuildSelectOptions(DataTable dt,string textField,string valueField){
			XmlDocument doc = new XmlDocument();
			doc.LoadXml("<SELECT/>");
			foreach( DataRow row in dt.Rows ){
				XmlElement el = doc.CreateElement("OPTION");
				el.SetAttribute("value",row[valueField]+"");
				el.InnerText = row[textField]+"";
				doc.DocumentElement.AppendChild(el);
			}
			return doc.DocumentElement.InnerXml;
		}
		*/
		#endregion
	}
}